{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "def get_all_filenames(file_dir):\n",
    "    all_files = [file for file in os.listdir(file_dir)]\n",
    "    # print(all_files)\n",
    "    return all_files\n",
    "def copy_files_to_directory(source_file, dest_dir):\n",
    "    if os.path.isfile(source_file):\n",
    "        # 使用shutil.copy2复制文件（包括元数据）\n",
    "        shutil.copy2(source_file, dest_dir)\n",
    "        # print(f\"Copied {source_file} to {dest_file}\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\Desktop\\heart_sound\\two_audios\\training-a\n",
      "a0001\n",
      "a0002\n",
      "1\n",
      "a0003\n",
      "1\n",
      "a0004\n",
      "1\n",
      "a0005\n",
      "1\n",
      "a0006\n",
      "1\n",
      "a0007\n",
      "-1\n",
      "a0008\n",
      "1\n",
      "a0009\n",
      "-1\n",
      "a0010\n",
      "1\n",
      "a0011\n",
      "-1\n",
      "a0012\n",
      "-1\n",
      "a0013\n",
      "1\n",
      "a0014\n",
      "1\n",
      "a0015\n",
      "1\n",
      "a0016\n",
      "-1\n",
      "a0017\n",
      "1\n",
      "a0018\n",
      "1\n",
      "a0019\n",
      "-1\n",
      "a0020\n",
      "1\n",
      "a0021\n",
      "1\n",
      "a0022\n",
      "1\n",
      "a0023\n",
      "1\n",
      "a0024\n",
      "1\n",
      "a0025\n",
      "-1\n",
      "a0026\n",
      "1\n",
      "a0027\n",
      "-1\n",
      "a0028\n",
      "-1\n",
      "a0029\n",
      "-1\n",
      "a0030\n",
      "1\n",
      "a0031\n",
      "1\n",
      "a0032\n",
      "-1\n",
      "a0033\n",
      "1\n",
      "a0034\n",
      "1\n",
      "a0035\n",
      "-1\n",
      "a0036\n",
      "1\n",
      "a0037\n",
      "1\n",
      "a0038\n",
      "-1\n",
      "a0039\n",
      "1\n",
      "a0040\n",
      "1\n",
      "a0041\n",
      "1\n",
      "a0042\n",
      "1\n",
      "a0043\n",
      "1\n",
      "a0044\n",
      "1\n",
      "a0045\n",
      "1\n",
      "a0046\n",
      "1\n",
      "a0047\n",
      "1\n",
      "a0048\n",
      "1\n",
      "a0049\n",
      "-1\n",
      "a0050\n",
      "-1\n",
      "a0051\n",
      "1\n",
      "a0052\n",
      "1\n",
      "a0053\n",
      "-1\n",
      "a0054\n",
      "1\n",
      "a0055\n",
      "-1\n",
      "a0056\n",
      "1\n",
      "a0057\n",
      "1\n",
      "a0058\n",
      "1\n",
      "a0059\n",
      "1\n",
      "a0060\n",
      "1\n",
      "a0061\n",
      "1\n",
      "a0062\n",
      "1\n",
      "a0063\n",
      "1\n",
      "a0064\n",
      "1\n",
      "a0065\n",
      "1\n",
      "a0066\n",
      "1\n",
      "a0067\n",
      "1\n",
      "a0068\n",
      "-1\n",
      "a0069\n",
      "-1\n",
      "a0070\n",
      "-1\n",
      "a0071\n",
      "-1\n",
      "a0072\n",
      "1\n",
      "a0073\n",
      "1\n",
      "a0074\n",
      "1\n",
      "a0075\n",
      "1\n",
      "a0076\n",
      "1\n",
      "a0077\n",
      "1\n",
      "a0078\n",
      "1\n",
      "a0079\n",
      "1\n",
      "a0080\n",
      "-1\n",
      "a0081\n",
      "-1\n",
      "a0082\n",
      "1\n",
      "a0083\n",
      "1\n",
      "a0084\n",
      "1\n",
      "a0085\n",
      "-1\n",
      "a0086\n",
      "-1\n",
      "a0087\n",
      "1\n",
      "a0088\n",
      "-1\n",
      "a0089\n",
      "1\n",
      "a0090\n",
      "1\n",
      "a0091\n",
      "-1\n",
      "a0092\n",
      "1\n",
      "a0093\n",
      "-1\n",
      "a0094\n",
      "-1\n",
      "a0095\n",
      "1\n",
      "a0096\n",
      "1\n",
      "a0097\n",
      "1\n",
      "a0098\n",
      "1\n",
      "a0099\n",
      "1\n",
      "a0100\n",
      "1\n",
      "a0101\n",
      "1\n",
      "a0102\n",
      "-1\n",
      "a0103\n",
      "1\n",
      "a0104\n",
      "1\n",
      "a0105\n",
      "-1\n",
      "a0106\n",
      "-1\n",
      "a0107\n",
      "1\n",
      "a0108\n",
      "-1\n",
      "a0109\n",
      "-1\n",
      "a0110\n",
      "1\n",
      "a0111\n",
      "1\n",
      "a0112\n",
      "1\n",
      "a0113\n",
      "1\n",
      "a0114\n",
      "1\n",
      "a0115\n",
      "1\n",
      "a0116\n",
      "1\n",
      "a0117\n",
      "1\n",
      "a0118\n",
      "-1\n",
      "a0119\n",
      "1\n",
      "a0120\n",
      "1\n",
      "a0121\n",
      "1\n",
      "a0122\n",
      "1\n",
      "a0123\n",
      "1\n",
      "a0124\n",
      "1\n",
      "a0125\n",
      "-1\n",
      "a0126\n",
      "1\n",
      "a0127\n",
      "-1\n",
      "a0128\n",
      "1\n",
      "a0129\n",
      "-1\n",
      "a0130\n",
      "1\n",
      "a0131\n",
      "1\n",
      "a0132\n",
      "1\n",
      "a0133\n",
      "1\n",
      "a0134\n",
      "1\n",
      "a0135\n",
      "1\n",
      "a0136\n",
      "-1\n",
      "a0137\n",
      "1\n",
      "a0138\n",
      "1\n",
      "a0139\n",
      "-1\n",
      "a0140\n",
      "-1\n",
      "a0141\n",
      "-1\n",
      "a0142\n",
      "1\n",
      "a0143\n",
      "1\n",
      "a0144\n",
      "1\n",
      "a0145\n",
      "1\n",
      "a0146\n",
      "1\n",
      "a0147\n",
      "1\n",
      "a0148\n",
      "1\n",
      "a0149\n",
      "1\n",
      "a0150\n",
      "1\n",
      "a0151\n",
      "1\n",
      "a0152\n",
      "1\n",
      "a0153\n",
      "-1\n",
      "a0154\n",
      "-1\n",
      "a0155\n",
      "-1\n",
      "a0156\n",
      "1\n",
      "a0157\n",
      "1\n",
      "a0158\n",
      "1\n",
      "a0159\n",
      "1\n",
      "a0160\n",
      "1\n",
      "a0161\n",
      "-1\n",
      "a0162\n",
      "1\n",
      "a0163\n",
      "1\n",
      "a0164\n",
      "1\n",
      "a0165\n",
      "-1\n",
      "a0166\n",
      "1\n",
      "a0167\n",
      "1\n",
      "a0168\n",
      "1\n",
      "a0169\n",
      "1\n",
      "a0170\n",
      "1\n",
      "a0171\n",
      "-1\n",
      "a0172\n",
      "1\n",
      "a0173\n",
      "1\n",
      "a0174\n",
      "1\n",
      "a0175\n",
      "1\n",
      "a0176\n",
      "1\n",
      "a0177\n",
      "1\n",
      "a0178\n",
      "-1\n",
      "a0179\n",
      "-1\n",
      "a0180\n",
      "1\n",
      "a0181\n",
      "-1\n",
      "a0182\n",
      "1\n",
      "a0183\n",
      "1\n",
      "a0184\n",
      "-1\n",
      "a0185\n",
      "-1\n",
      "a0186\n",
      "1\n",
      "a0187\n",
      "1\n",
      "a0188\n",
      "1\n",
      "a0189\n",
      "-1\n",
      "a0190\n",
      "1\n",
      "a0191\n",
      "1\n",
      "a0192\n",
      "1\n",
      "a0193\n",
      "-1\n",
      "a0194\n",
      "1\n",
      "a0195\n",
      "-1\n",
      "a0196\n",
      "-1\n",
      "a0197\n",
      "1\n",
      "a0198\n",
      "1\n",
      "a0199\n",
      "-1\n",
      "a0200\n",
      "1\n",
      "a0201\n",
      "1\n",
      "a0202\n",
      "1\n",
      "a0203\n",
      "1\n",
      "a0204\n",
      "-1\n",
      "a0205\n",
      "1\n",
      "a0206\n",
      "1\n",
      "a0207\n",
      "1\n",
      "a0208\n",
      "-1\n",
      "a0209\n",
      "1\n",
      "a0210\n",
      "-1\n",
      "a0211\n",
      "-1\n",
      "a0212\n",
      "-1\n",
      "a0213\n",
      "1\n",
      "a0214\n",
      "1\n",
      "a0215\n",
      "1\n",
      "a0216\n",
      "1\n",
      "a0217\n",
      "1\n",
      "a0218\n",
      "1\n",
      "a0219\n",
      "1\n",
      "a0220\n",
      "1\n",
      "a0221\n",
      "1\n",
      "a0222\n",
      "1\n",
      "a0223\n",
      "1\n",
      "a0224\n",
      "1\n",
      "a0225\n",
      "1\n",
      "a0226\n",
      "1\n",
      "a0227\n",
      "-1\n",
      "a0228\n",
      "1\n",
      "a0229\n",
      "-1\n",
      "a0230\n",
      "1\n",
      "a0231\n",
      "-1\n",
      "a0232\n",
      "1\n",
      "a0233\n",
      "1\n",
      "a0234\n",
      "1\n",
      "a0235\n",
      "-1\n",
      "a0236\n",
      "1\n",
      "a0237\n",
      "1\n",
      "a0238\n",
      "-1\n",
      "a0239\n",
      "1\n",
      "a0240\n",
      "1\n",
      "a0241\n",
      "-1\n",
      "a0242\n",
      "1\n",
      "a0243\n",
      "-1\n",
      "a0244\n",
      "1\n",
      "a0245\n",
      "1\n",
      "a0246\n",
      "1\n",
      "a0247\n",
      "1\n",
      "a0248\n",
      "1\n",
      "a0249\n",
      "1\n",
      "a0250\n",
      "-1\n",
      "a0251\n",
      "1\n",
      "a0252\n",
      "1\n",
      "a0253\n",
      "-1\n",
      "a0254\n",
      "1\n",
      "a0255\n",
      "1\n",
      "a0256\n",
      "1\n",
      "a0257\n",
      "1\n",
      "a0258\n",
      "1\n",
      "a0259\n",
      "1\n",
      "a0260\n",
      "1\n",
      "a0261\n",
      "1\n",
      "a0262\n",
      "1\n",
      "a0263\n",
      "1\n",
      "a0264\n",
      "-1\n",
      "a0265\n",
      "1\n",
      "a0266\n",
      "-1\n",
      "a0267\n",
      "1\n",
      "a0268\n",
      "1\n",
      "a0269\n",
      "1\n",
      "a0270\n",
      "-1\n",
      "a0271\n",
      "-1\n",
      "a0272\n",
      "1\n",
      "a0273\n",
      "1\n",
      "a0274\n",
      "-1\n",
      "a0275\n",
      "1\n",
      "a0276\n",
      "1\n",
      "a0277\n",
      "1\n",
      "a0278\n",
      "1\n",
      "a0279\n",
      "1\n",
      "a0280\n",
      "1\n",
      "a0281\n",
      "1\n",
      "a0282\n",
      "1\n",
      "a0283\n",
      "-1\n",
      "a0284\n",
      "1\n",
      "a0285\n",
      "-1\n",
      "a0286\n",
      "1\n",
      "a0287\n",
      "-1\n",
      "a0288\n",
      "1\n",
      "a0289\n",
      "-1\n",
      "a0290\n",
      "-1\n",
      "a0291\n",
      "1\n",
      "a0292\n",
      "1\n",
      "a0293\n",
      "-1\n",
      "a0294\n",
      "-1\n",
      "a0295\n",
      "1\n",
      "a0296\n",
      "1\n",
      "a0297\n",
      "1\n",
      "a0298\n",
      "-1\n",
      "a0299\n",
      "-1\n",
      "a0300\n",
      "1\n",
      "a0301\n",
      "-1\n",
      "a0302\n",
      "-1\n",
      "a0303\n",
      "1\n",
      "a0304\n",
      "-1\n",
      "a0305\n",
      "1\n",
      "a0306\n",
      "1\n",
      "a0307\n",
      "1\n",
      "a0308\n",
      "1\n",
      "a0309\n",
      "-1\n",
      "a0310\n",
      "-1\n",
      "a0311\n",
      "-1\n",
      "a0312\n",
      "1\n",
      "a0313\n",
      "1\n",
      "a0314\n",
      "1\n",
      "a0315\n",
      "1\n",
      "a0316\n",
      "1\n",
      "a0317\n",
      "1\n",
      "a0318\n",
      "1\n",
      "a0319\n",
      "1\n",
      "a0320\n",
      "1\n",
      "a0321\n",
      "1\n",
      "a0322\n",
      "1\n",
      "a0323\n",
      "-1\n",
      "a0324\n",
      "1\n",
      "a0325\n",
      "-1\n",
      "a0326\n",
      "1\n",
      "a0327\n",
      "1\n",
      "a0328\n",
      "-1\n",
      "a0329\n",
      "-1\n",
      "a0330\n",
      "-1\n",
      "a0331\n",
      "1\n",
      "a0332\n",
      "-1\n",
      "a0333\n",
      "1\n",
      "a0334\n",
      "-1\n",
      "a0335\n",
      "1\n",
      "a0336\n",
      "1\n",
      "a0337\n",
      "-1\n",
      "a0338\n",
      "1\n",
      "a0339\n",
      "1\n",
      "a0340\n",
      "-1\n",
      "a0341\n",
      "1\n",
      "a0342\n",
      "-1\n",
      "a0343\n",
      "1\n",
      "a0344\n",
      "1\n",
      "a0345\n",
      "1\n",
      "a0346\n",
      "1\n",
      "a0347\n",
      "1\n",
      "a0348\n",
      "1\n",
      "a0349\n",
      "1\n",
      "a0350\n",
      "1\n",
      "a0351\n",
      "-1\n",
      "a0352\n",
      "-1\n",
      "a0353\n",
      "-1\n",
      "a0354\n",
      "1\n",
      "a0355\n",
      "-1\n",
      "a0356\n",
      "1\n",
      "a0357\n",
      "1\n",
      "a0358\n",
      "1\n",
      "a0359\n",
      "-1\n",
      "a0360\n",
      "1\n",
      "a0361\n",
      "-1\n",
      "a0362\n",
      "1\n",
      "a0363\n",
      "1\n",
      "a0364\n",
      "1\n",
      "a0365\n",
      "1\n",
      "a0366\n",
      "1\n",
      "a0367\n",
      "1\n",
      "a0368\n",
      "1\n",
      "a0369\n",
      "1\n",
      "a0370\n",
      "1\n",
      "a0371\n",
      "1\n",
      "a0372\n",
      "1\n",
      "a0373\n",
      "1\n",
      "a0374\n",
      "1\n",
      "a0375\n",
      "1\n",
      "a0376\n",
      "1\n",
      "a0377\n",
      "1\n",
      "a0378\n",
      "1\n",
      "a0379\n",
      "1\n",
      "a0380\n",
      "1\n",
      "a0381\n",
      "1\n",
      "a0382\n",
      "1\n",
      "a0383\n",
      "1\n",
      "a0384\n",
      "1\n",
      "a0385\n",
      "-1\n",
      "a0386\n",
      "-1\n",
      "a0387\n",
      "1\n",
      "a0388\n",
      "1\n",
      "a0389\n",
      "1\n",
      "a0390\n",
      "1\n",
      "a0391\n",
      "1\n",
      "a0392\n",
      "1\n",
      "a0393\n",
      "-1\n",
      "a0394\n",
      "1\n",
      "a0395\n",
      "1\n",
      "a0396\n",
      "1\n",
      "a0397\n",
      "1\n",
      "a0398\n",
      "1\n",
      "a0399\n",
      "1\n",
      "a0400\n",
      "1\n",
      "a0401\n",
      "-1\n",
      "a0402\n",
      "1\n",
      "a0403\n",
      "1\n",
      "a0404\n",
      "1\n",
      "a0405\n",
      "-1\n",
      "a0406\n",
      "-1\n",
      "a0407\n",
      "-1\n",
      "a0408\n",
      "-1\n",
      "a0409\n",
      "-1\n",
      "C:\\Users\\Administrator\\Desktop\\heart_sound\\two_audios\\training-b\n",
      "b0001\n",
      "C:\\Users\\Administrator\\Desktop\\heart_sound\\two_audios\\training-c\n",
      "c0001\n",
      "C:\\Users\\Administrator\\Desktop\\heart_sound\\two_audios\\training-d\n",
      "d0001\n",
      "C:\\Users\\Administrator\\Desktop\\heart_sound\\two_audios\\training-e\n",
      "e00001\n",
      "C:\\Users\\Administrator\\Desktop\\heart_sound\\two_audios\\training-f\n",
      "f0001\n"
     ]
    }
   ],
   "source": [
    "#training-a training-b training-c training-d training-e training-f\n",
    "csv_file_path = 'C:\\\\Users\\\\Administrator\\\\Desktop\\\\heart_sound\\\\two_audios\\\\training-a\\\\REFERENCE.csv'\n",
    "columns = ['file','dir']\n",
    "df = pd.read_csv(csv_file_path)\n",
    "df.columns = columns\n",
    "classifications = dict(zip(df[\"file\"], df[\"dir\"]))\n",
    "file_wav = []\n",
    "file_dir = \"C:\\\\Users\\\\Administrator\\\\Desktop\\\\heart_sound\\\\two_audios\"\n",
    "dir_name = get_all_filenames(file_dir)\n",
    "# print(len(dir_name))\n",
    "for i in range(len(dir_name)):\n",
    "    path = os.path.join(file_dir, dir_name[i])\n",
    "    print(path)\n",
    "    file_name = get_all_filenames(path)\n",
    "    # print(len(file_name))\n",
    "    for x in range(len(file_name)):\n",
    "        if \"wav\" in file_name[x]:\n",
    "            path_wav = os.path.join(path, file_name[x])\n",
    "            wav_name = file_name[x].replace(\".wav\", \"\")\n",
    "            print(wav_name)\n",
    "            if wav_name in classifications:\n",
    "                target_folder = classifications[wav_name]\n",
    "                print(target_folder)\n",
    "                target_folder_path = os.path.join(\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\heart_sound\\\\build\\\\two_audios\",str(target_folder))\n",
    "                if not os.path.exists(target_folder_path):\n",
    "                    os.makedirs(target_folder_path)\n",
    "                shutil.move(path_wav,target_folder_path)\n",
    "        if \"wav\" in file_name[x]:\n",
    "            path_wav = os.path.join(path, file_name[x])\n",
    "            copy_files_to_directory(path_wav,\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\heart_sound\\\\build\\\\two_audios\\\\file_audio\")\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
